﻿using System;
using System.Collections.Generic;
using System.Linq;
namespace DataModel.Commands
{
    public interface ICondition
    {
        bool value(object parameter);
        /// <summary>
        /// Adds a handler using a weak reference 
        /// </summary>
        /// <param name="handler"></param>
        void AddHandler(ConditionCommand handler);
    }
    public abstract class staticCondition:ICondition
    {
        static List<WeakReference> handlers = new List<WeakReference>();
        static IEnumerable<ConditionCommand> Handlers { get { return handlers.Where((W) => W.IsAlive).Select((W) => (ConditionCommand)W.Target); } }

        public static void OnConditionChanged()
        {
            foreach (var item in Handlers)
            {
                item.onCanExecuteChanged();
            }
        }

        #region ICondition Members

        public abstract bool value(object parameter);

        public void AddHandler(ConditionCommand handler)
        {
            handlers.Add(new WeakReference(handler));
        }

        #endregion
    }
    public abstract class instanceCondition : ICondition
    {
        List<ConditionCommand> Handlers = new List<ConditionCommand>();
        public void OnConditionChanged()
        {
            foreach (var item in Handlers)
            {
                item.onCanExecuteChanged();
            }
        }

        #region ICondition Members

        public abstract bool value(object parameter);

        public void AddHandler(ConditionCommand handler)
        {
            Handlers.Add(handler);
        }

        #endregion
    }
}
